package com.dianping.cat.home.service.client.transform;

import com.dianping.cat.home.service.client.IEntity;
import com.dianping.cat.home.service.client.IVisitor;
import com.dianping.cat.home.service.client.entity.ClientReport;
import com.dianping.cat.home.service.client.entity.Domain;
import com.dianping.cat.home.service.client.entity.Method;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;


















public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  39 */   public DefaultXmlBuilder() { this(false); }



/*  43 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  47 */     this.m_compact = compact;
/*  48 */     this.m_sb = sb;
/*  49 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  53 */     entity.accept(this.m_visitor);
/*  54 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  58 */     this.m_level--;

/*  60 */     indent();
/*  61 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  65 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  69 */     if (value == null) {
/*  70 */       return null;
    }

/*  73 */     String str = toString(value);
/*  74 */     int len = str.length();
/*  75 */     StringBuilder sb = new StringBuilder(len + 16);

/*  77 */     for (int i = 0; i < len; i++) {
      char ch = str.charAt(i);

/*  80 */       switch (ch) {
        case '<':
/*  82 */           sb.append("&lt;");
          break;
        case '>':
/*  85 */           sb.append("&gt;");
          break;
        case '&':
/*  88 */           sb.append("&amp;");
          break;
        case '"':
/*  91 */           if (!text) {
/*  92 */             sb.append("&quot;");
            break;
          }
        default:
/*  96 */           sb.append(ch);
          break;
      }

    }
/* 101 */     return sb.toString();
  }

  protected void indent() {
/* 105 */     if (!this.m_compact) {
/* 106 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 107 */         this.m_sb.append("   ");
      }
    }
  }


/* 113 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 117 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 121 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 125 */     indent();

/* 127 */     this.m_sb.append('<').append(name);

/* 129 */     int len = nameValues.length;

/* 131 */     for (int i = 0; i + 1 < len; i += 2) {
/* 132 */       Object attrName = nameValues[i];
/* 133 */       Object attrValue = nameValues[i + 1];

/* 135 */       if (attrValue != null) {
/* 136 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 140 */     if (dynamicAttributes != null) {
/* 141 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 142 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 146 */     if (text != null && closed) {
/* 147 */       this.m_sb.append('>');
/* 148 */       this.m_sb.append(escape(text, true));
/* 149 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 151 */       if (closed) {
/* 152 */         this.m_sb.append('/');
      } else {
/* 154 */         this.m_level++;
      }

/* 157 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 163 */     if (value instanceof String)
/* 164 */       return (String)value; 
/* 165 */     if (value instanceof Collection) {
/* 166 */       Collection<Object> list = (Collection)value;
/* 167 */       StringBuilder sb = new StringBuilder(32);
/* 168 */       boolean first = true;

/* 170 */       for (Object item : list) {
/* 171 */         if (first) {
/* 172 */           first = false;
        } else {
/* 174 */           sb.append(',');
        }

/* 177 */         if (item != null) {
/* 178 */           sb.append(item);
        }
      }

/* 182 */       return sb.toString();
/* 183 */     }  if (value.getClass().isArray()) {
/* 184 */       int len = Array.getLength(value);
/* 185 */       StringBuilder sb = new StringBuilder(32);
/* 186 */       boolean first = true;

/* 188 */       for (int i = 0; i < len; i++) {
/* 189 */         Object item = Array.get(value, i);

/* 191 */         if (first) {
/* 192 */           first = false;
        } else {
/* 194 */           sb.append(',');
        }

/* 197 */         if (item != null) {
/* 198 */           sb.append(item);
        }
      }

/* 202 */       return sb.toString();
    }

/* 205 */     return String.valueOf(value);
  }

  protected String toString(Date date, String format) {
/* 209 */     if (date != null) {
/* 210 */       return (new SimpleDateFormat(format)).format(date);
    }
/* 212 */     return null;
  }



  public void visitClientReport(ClientReport clientReport) {
/* 218 */     startTag("client-report", null, new Object[] { "domain", clientReport.getDomain(), "start-time", toString(clientReport.getStartTime(), "yyyy-MM-dd HH:mm:ss"), "end-time", toString(clientReport.getEndTime(), "yyyy-MM-dd HH:mm:ss") });

/* 220 */     if (!clientReport.getDomains().isEmpty()) {
/* 221 */       for (Domain domain : clientReport.getDomains().values()) {
/* 222 */         domain.accept(this.m_visitor);
      }
    }

/* 226 */     endTag("client-report");
  }


  public void visitDomain(Domain domain) {
/* 231 */     startTag("domain", null, new Object[] { "id", domain.getId(), "totalCount", Long.valueOf(domain.getTotalCount()), "failureCount", Long.valueOf(domain.getFailureCount()), "failurePercent", Double.valueOf(domain.getFailurePercent()), "sum", Double.valueOf(domain.getSum()), "avg", Double.valueOf(domain.getAvg()) });

/* 233 */     if (!domain.getMethods().isEmpty()) {
/* 234 */       for (Method method : domain.getMethods().values()) {
/* 235 */         method.accept(this.m_visitor);
      }
    }

/* 239 */     endTag("domain");
  }



/* 244 */   public void visitMethod(Method method) { startTag("method", true, null, new Object[] { "id", method.getId(), "service", method.getService(), "totalCount", Long.valueOf(method.getTotalCount()), "failureCount", Long.valueOf(method.getFailureCount()), "failurePercent", Double.valueOf(method.getFailurePercent()), "sum", Double.valueOf(method.getSum()), "avg", Double.valueOf(method.getAvg()), "qps", Double.valueOf(method.getQps()), "timeout", Double.valueOf(method.getTimeout()) }); }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/service/client/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */